package com.wtb.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import com.wtb.qq.domain.QqOauth;

public class QqXmlUtils {
	/**
	 * 写入一条新数据到XML中
	 * 
	 * @param document
	 * @throws IOException
	 */
	public static  Document addToXml(Document document, QqOauth qqOauth ){
		Element root = document.getRootElement();
		Element userElement = root.addElement("user");
		userElement.addElement("oauth_token").addText(
				qqOauth.getOauth_token());
		userElement.addElement("oauth_token_secrete").addText(
				qqOauth.getOauth_token_secrete());
		userElement.addElement("createTime").addText(
				String.valueOf(qqOauth.getCreateTime()));
		userElement.addElement("uni").addText(
				String.valueOf(qqOauth.getUni()));
		userElement.addElement("ref").addText(
				String.valueOf(qqOauth.getRef()));
		return document;
	}

	/**
	 * 读取所有XML内容 放入HashMap中，便于查找
	 * 
	 * @param document
	 * @return
	 */
	
	@SuppressWarnings("unchecked")
	public static Map<String, QqOauth> readFromXml(Document document) {
		Map<String, QqOauth> qqOauthMap = new HashMap<String, QqOauth>();
		List<Node> list = document.selectNodes("/users/user");
		if (list != null) {
			for (Node node : list) {
				String oauth_token = node.selectSingleNode("./oauth_token")
						.getStringValue();
				String oauth_token_secrete = node.selectSingleNode("./oauth_token_secrete")
						.getStringValue();
				String createTime = node.selectSingleNode("./createTime").getStringValue();
				String uni = node.selectSingleNode("./uni").getStringValue();
				String ref = node.selectSingleNode("./ref").getStringValue();
				QqOauth qqOauth = new QqOauth(oauth_token, oauth_token_secrete,createTime,uni,ref);
				qqOauthMap.put(uni, qqOauth);
			}
		}
		return qqOauthMap;
	}

	/**
	 * 修改XML中的一条数据
	 * 
	 * @param document
	 * @param oauthBean
	 * @throws IOException
	 */
	public static  Document modifyXml(Document document, QqOauth qqOauth){
		Element element = (Element) document.selectSingleNode("/users/user/uni[contains(text(),'"+qqOauth.getUni()+"'] ");
		Element oauth_token = (Element) element.getParent().selectSingleNode(
				"./oauth_token");
		Element oauth_token_secrete = (Element) element.getParent().selectSingleNode(
				"./oauth_token_secrete");
		Element createTime = (Element) element.getParent().selectSingleNode(
				"./createTime");
		Element uni = (Element) element.getParent().selectSingleNode(
				"./uni");
		Element ref = (Element) element.getParent().selectSingleNode(
				"./ref");
		if (!oauth_token.getStringValue().equals(qqOauth.getOauth_token())) {
			oauth_token.setText(qqOauth.getOauth_token());
		}
		if (!oauth_token_secrete.getStringValue().equals(qqOauth.getOauth_token_secrete())) {
			oauth_token_secrete.setText(qqOauth.getOauth_token_secrete());
		}
		if (!createTime.getStringValue().equals(qqOauth.getCreateTime())) {
			createTime.setText(qqOauth.getCreateTime());
		}
		if (!uni.getStringValue().equals(qqOauth.getUni())) {
			uni.setText(qqOauth.getUni());
		}
		if (!ref.getStringValue().equals(qqOauth.getRef())) {
			ref.setText(qqOauth.getRef());
		}
		return document;
	}

	/**
	 * 删除XML中的一条数据
	 * 
	 * @param document
	 * @param oauthBean
	 * @throws IOException
	 */
	public static  Document delFroXml(Document document, QqOauth qqOauth){
		Element element = (Element) document.selectSingleNode("/users/user/uni[contains(text(),'"+qqOauth.getUni()+"'] ");
		document.getRootElement().remove(element);
		return document;
	}

}
